//T10083. 合并果子
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN=30000;
int n;
int a[MAXN+1];
int ans;
int main(){
    cin>>n;
    for(int i=0;i<n;i++) cin>>a[i];
    for(int i=0;i<n;i++) push_heap(a,a+i+1,greater<int>());
    int size=n;
    while(size>1){
        pop_heap(a,a+size,greater<int>());
        int s1=a[size-1];
        size--;
        pop_heap(a,a+size,greater<int>());
        int s2=a[size-1];
        size--;
        ans+=s1+s2;
        a[size]=s1+s2;
        size++;
        push_heap(a,a+size,greater<int>());
    }
    cout<<ans;
}